<?php


if (isset($_GET['action']) && $_GET['action'] == "unsign" && isset($_GET['rid'])) {

				$raid_id = $_GET['rid'];

				$unsign_raid = $raid->get($raid_id);

				$old_signup = $signup->get($raid_id, -1, -1, $_SESSION['uid']);

				$old_signup = $old_signup[0];

				$signup->save($old_signup['turnin_id'], $old_signup['player_id'], $raid_id, $old_signup['substitute'], 1,  $old_signup['signup_date'], 'Now()',  $old_signup['comment']);
}



if (isset($_GET['action']) && $_GET['action'] == "activate" && isset($_GET['pid'])) {

	$player->deactivate($_SESSION['uid']);
	$player->activate($_GET['pid'], $_SESSION['uid']);
}


if (isset($_POST['action']) && $_POST['action'] == "usersave" && isset($_POST['name']) && isset($_POST['username']) && isset($_POST['email']) && isset($_POST['password1']) && isset($_POST['password2'])) {

    	global $_GLOBALSALT;

		$u_id = abs($_SESSION['uid']);
		$u_name = $_POST['name'];
		$u_username= $_POST['username'];
		$u_password1 = $_POST['password1'];
		$u_password2 = $_POST['password2'];
		$u_level = abs($_SESSION['level']);
		$u_email = $_POST['email'];
		$u_class_display = $_POST['class_display'];

		if (!empty($u_password1) && !empty($u_password2) && ($u_password1 == $u_password2)) {

			$salt = create_salt(5);

			$password = $u_password1;

            $user->save($u_id, $u_name, $u_email, $password, $u_username, $u_level, $salt, $u_class_display);

            $useredit_errormessage = "User update succesfull<br />Please relogin now to make the changes final.";
		}
		elseif (empty($u_password1) && empty($u_password2)) 
		{
			
            $user->save($u_id, $u_name, $u_email, 0, $u_username, $u_level, $salt, $u_class_display);

            $useredit_errormessage = "User update succesfull<br />Please relogin now to make the changes final.";

		}
		else
		{
			
		    $useredit_errormessage = "The filled in passwords were not the same";

		    $smarty->assign('useredit_errormessage', $useredit_errormessage);
		}



}

if (isset($_GET['action']) && $_GET['action'] == "remove" && $player->isOwner($_GET['player_id']))  {

	$player_id = $_GET['player_id'];

	$player->remove($player_id);

	$smarty->assign('message', "The player has been removed!");

}

if (isset($_GET['action']) && $_GET['action'] == "new") {

    $attributes = $player_attribute->get($_SESSION['uid']);


    
    
    $attribute_categories = $attribute->get();

    $smarty->assign('attributes', $attributes);
    $smarty->assign('attribute_categories', $attribute_categories);
	$smarty->assign('races', $races);
	$smarty->assign('classes', $classes);

	$smarty->display('panel_edit.tpl', $_SESSION['uid']);

} elseif (isset($_GET['action']) && $_GET['action'] == "edit" && $player->isOwner($_GET['pid'])) {

	$player_id = $_GET['pid'];

	$players = $player->get($player_id);

	$edit_player = $players[0];
	
	$class_roles = $class_role->get(0, $edit_player['class_id']);

	$smarty->assign('player', $edit_player);

    $attributes = $player_attribute->get($_SESSION['uid']);

    $attribute_categories = $attribute->get();

    $smarty->assign('attributes', $attributes);
    $smarty->assign('attribute_categories', $attribute_categories);
	$smarty->assign('races', $races);
	$smarty->assign('classes', $classes);
	$smarty->assign('class_roles', $class_roles);

	$smarty->display('panel_edit.tpl', $_SESSION['uid']);



} elseif (isset($_POST['action']) && $_POST['action'] == "save") {

    $attribute_categories = $attribute->get();

	$validated		= 1;
	$name			= '';
	$class			= 0;
	$race			= '';
	$role			= 0;

	if (!empty($_POST['name']))
	{
		$name = $_POST['name'];

	}
	else
	{
		$smarty->assign('name_error', "Please insert an name.");
		$validated = 0;
	}

	if (!empty($_POST['class']))
	{
		$class = $_POST['class'];


	}
	else
	{
		$smarty->assign('class_error', "Please insert an class.");
		$validated = 0;
	}

	if (!empty($_POST['race']))
	{
		$race = $_POST['race'];


	}
	else
	{
		$smarty->assign('race_error', "Please insert an race.");
		$validated = 0;
	}

	foreach($attribute_categories as $key => $single_attribute) {

	    $update_attributes[$key][attribute_id] = $single_attribute[id];

	    $update_attributes[$key][update] = $_POST['update' . $single_attribute[id]];

	    if (!empty($_POST['attriplayer' . $single_attribute[id]])) {
    	    $update_attributes[$key][attribute_player_id] = $_POST['attriplayer' . $single_attribute[id]];
	    } else {
            $update_attributes[$key][attribute_player_id] = 0;
	    }

	    if ($single_attribute[type] == 1)
	    {

	        $update_attributes[$key][type] = 1;

	        if($_POST['attri' . $single_attribute[id]] == 'on')
	        {
	            $update_attributes[$key][value] = 1;
	        } else
	        {
	            $update_attributes[$key][value] = 0;
	        }
	    }
	     elseif ($single_attribute[type] == 2)
	    {
	    $update_attributes[$key][type] = 2;

	    	if (!empty($_POST['attri' . $single_attribute[id]]))
	    	{
	    	    $update_attributes[$key][value] = $_POST['attri' . $single_attribute[id]];
	    	}
	    	else
	    	{
	    	    $update_attributes[$key][value] = 0;
	    	}

	    }
	}

	if ($validated == 0) {

		$smarty->display('panel_edit.tpl', $_SESSION['uid']);

	} else {

		$role_id = $_POST['role'];
		
	    if(isset($_POST['player_id']) && $_POST['player_id'] > 0) {
    	    $player_id = $_POST['player_id'];
    	    
    	    $old_player = $player->get($player_id);

    	    $old_player = $old_player[0];
			
    	    if ($player->isOwner($player_id)) {
    	       $player->save($player_id, $_SESSION['uid'], $name, $class, $race, 70, $old_player[team_id], 0, 0 ,0 , $role_id);
    	    }
	    } else {
	        $player_id = $player->save(0, $_SESSION['uid'], $name, $class, $race, 70, 1, 0, 0 ,0 , $role_id);
	        
			$profile->getArmory($name, $player_id);
			
	    }
        if(!empty($update_attributes)) {
        	    foreach($update_attributes as $new_attribute)
        	    {
        	       $player_attribute->save($new_attribute[attribute_player_id], $new_attribute[attribute_id], $player_id, $new_attribute[value]);
        	    }
        }



$user_information = $user->get($_SESSION['uid']);

$user_information = $user_information[0];

//debug_array($user_information);

$active_character = $player->get( 0, "", 1, $_SESSION['uid']);
$inactive_character = $player->get( 0, "", 0, $_SESSION['uid']);

$points = $rp->get();

$rp_categories = $rp_category->get();

$player_raids = $raid->get(0,0,0,$_SESSION['uid']);

$attributes = $player_attribute->get();

$attribute_categories = $attribute->get();


foreach ($player_raids as $id => $raid) {
	$main_signups = $signup->count($raid['raid_id'], 0, 0);
	$main_subs = $signup->count($raid['raid_id'], 0, 1);

	$player_raids[$id]['signups'] = $main_signups[0]['sign_ups'];
	$player_raids[$id]['subs'] = $main_subs[0]['sign_ups'];

	$start_time = strtotime(str_replace("-","/",$raid['date_time']));

	$freeze_time = $raid['freeze_time'] * 60 * 60;

	$player_raids[$id]['close_time'] = $start_time - $freeze_time;

}

$current_time = time();

$smarty->assign('rp', $points);

$smarty->assign('rp_categories', $rp_categories);

$smarty->assign('attributes', $attributes);

$smarty->assign('attribute_categories', $attribute_categories);

$smarty->assign('user_information', $user_information);

$smarty->assign('active_character', $active_character);

$smarty->assign('inactive_character', $inactive_character);

$smarty->assign('player_raids', $player_raids);

$smarty->assign('current_time', $current_time);

$smarty->display('panel.tpl', $_SESSION['uid']);


	}

} else {


$user_information = $user->get($_SESSION['uid']);

$user_information = $user_information[0];

//debug_array($user_information);

$active_character = $player->get( 0, "", 1, $_SESSION['uid']);
$inactive_character = $player->get( 0, "", 0, $_SESSION['uid']);

$points = $rp->get();

$rp_categories = $rp_category->get();

$player_raids = $raid->get(0,0,0,$_SESSION['uid']);

$attributes = $player_attribute->get($_SESSION['uid']);

$attribute_categories = $attribute->get();

foreach ($player_raids as $id => $raid) {
	$main_signups = $signup->count($raid['raid_id'], 0, 0);
	$main_subs = $signup->count($raid['raid_id'], 0, 1);

	$player_raids[$id]['signups'] = $main_signups[0]['sign_ups'];
	$player_raids[$id]['subs'] = $main_subs[0]['sign_ups'];

	$start_time = strtotime(str_replace("-","/",$raid['date_time']));

	$freeze_time = $raid['freeze_time'] * 60 * 60;

	$player_raids[$id]['close_time'] = $start_time - $freeze_time;

}

$current_time = time();

$smarty->assign('rp', $points);

$smarty->assign('rp_categories', $rp_categories);

$smarty->assign('attributes', $attributes);

$smarty->assign('attribute_categories', $attribute_categories);

$smarty->assign('user_information', $user_information);

$smarty->assign('active_character', $active_character);

$smarty->assign('inactive_character', $inactive_character);

$smarty->assign('player_raids', $player_raids);

$smarty->assign('current_time', $current_time);

$smarty->display('panel.tpl', $_SESSION['uid']);


}

?>